<template>
  <div>
    <el-form ref="dispatchRef" :inline="true" :model="dispatchF.dispatchS">
      <el-form-item label="名称：" prop="name">
        <el-input v-model="dispatchF.dispatchS.name" placeholder="名称" type="text"></el-input>
      </el-form-item>
      <el-form-item label="分组" prop="phone">
        <el-input v-model="dispatchF.dispatchS.dispatchGroup" placeholder="分组" type="text"></el-input>
      </el-form-item>
      <el-form-item label="系统类型" prop="type">
        <el-select v-model="dispatchF.dispatchS.sysName" placeholder="系统类型">
          <el-option v-for="item in sysTypeF.sysType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="请求类型" prop="type">
        <el-select v-model="dispatchF.dispatchS.requestType" placeholder="请求类型">
          <el-option v-for="item in requestTypeF.requestType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="定时任务类型" prop="type">
        <el-select v-model="dispatchF.dispatchS.type" placeholder="定时任务类型">
          <el-option v-for="item in dispatchF.JobType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="startEndTime">
        <el-date-picker
          v-model="dispatchF.dispatchS.startTime"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          range-separator="-"
          start-placeholder="开始时间"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">搜索</el-button>
        <el-button type="primary" @click="resetS(dispatchRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog">
          <el-icon>
            <Plus />
          </el-icon>
          添加调度
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="dispatchF.tableData" stripe style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="80">
        <template #default="scope">
          {{ (dispatchF.dispatchS.page - 1) * dispatchF.dispatchS.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="名称" prop="name" />
      <el-table-column align="center" header-align="center" label="分组" prop="dispatchGroup">
        <template #default="scope">
          <el-tag close-transition>{{ scope.row.dispatchGroup }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" align="center" header-align="center" label="类名" prop="className" />
      <el-table-column align="center" header-align="center" label="系统名" prop="sysName.desc" />
      <el-table-column align="center" header-align="center" label="类型" prop="type.desc" />
      <el-table-column align="center" header-align="center" label="执行次数" prop="counts" />
      <el-table-column align="center" header-align="center" label="是否开启" prop="open">
        <template #default="scope">
          <el-switch v-model="scope.row.open" :active-value="true" :inactive-value="false" active-text="是" inactive-text="否" inline-prompt @change="chengStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" header-align="center" label="操作">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="edit(scope.row.id)">编辑 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deletes(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="dispatchF.dialogFormVisible" :title="dispatchF.title" destroy-on-close @close="resetE(editRef)">
      <el-form ref="editRef" :model="dispatchF.dispatchEdit" :rules="dispatchF.addRules">
        <el-row :gutter="10" class="row-bg">
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="dispatchF.dispatchEdit.name" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组名" prop="dispatchGroup">
              <el-input v-model="dispatchF.dispatchEdit.dispatchGroup" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类名" prop="className">
              <el-input v-model="dispatchF.dispatchEdit.className" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址" prop="url">
              <el-input v-model="dispatchF.dispatchEdit.url" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="系统类型" prop="sysName">
              <el-select v-model="dispatchF.dispatchEdit.sysName" placeholder="系统类型">
                <el-option v-for="item in sysTypeF.sysType" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="请求类型" prop="requestType">
              <el-select v-model="dispatchF.dispatchEdit.requestType" placeholder="请求类型">
                <el-option v-for="item in requestTypeF.requestType" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="定时任务类型" prop="type">
              <el-select v-model="dispatchF.dispatchEdit.type" placeholder="定时任务类型">
                <el-option v-for="item in dispatchF.JobType" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束时间" prop="stopTime">
              <el-date-picker v-model="dispatchF.dispatchEdit.stopTime" format="YYYY-MM-DD HH:mm:ss" placeholder="结束时间" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="请求登录用户" prop="loginName">
              <el-input v-model="dispatchF.dispatchEdit.loginName" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="cron" prop="cron">
              <Cron v-model:cron="dispatchF.dispatchEdit.cron"></Cron>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数" prop="param">
              <JsonEdit v-model:jsonContent="dispatchF.dispatchEdit.param" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求头" prop="header">
              <JsonEdit v-model:jsonContent="dispatchF.dispatchEdit.header" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item autocomplete="off" label="权重" prop="weight">
              <el-input-number v-model="dispatchF.dispatchEdit.weight" :max="10000" :min="0" :step="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item autocomplete="off" label="是否开启" prop="open">
              <el-switch v-model="dispatchF.dispatchEdit.open" :active-value="true" :inactive-value="false" active-text="是" inactive-text="否" inline-prompt />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item autocomplete="off" label="简介" prop="remark">
              <Editor v-model:editorValue="dispatchF.dispatchEdit.remark"></Editor>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <el-button type="primary" @click="save">保存 </el-button>
              <el-button @click="dispatchF.dialogFormVisible = false">取消 </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
  <Page :pageData="dispatchF.dispatchS" @selectPage="search"></Page>
</template>
<script lang="ts" setup>
import Page from '@/components/Page.vue'
import { dispatchClass, dispatchEditInfo, dispatchInfo } from '@/page/types/dispatch/dispatch'
import { requestTypeClass } from '@/page/types/dispatch/requestType'
import { sysTypeClass } from '@/page/types/dispatch/sysType'
import { ElMessage, FormInstance } from 'element-plus/es'
import Editor from '@/components/MyEditor.vue'
import { dispatchPage, dispatchSave, dispatchUpdate, dispatchPauseOrRes, deleteDispatch, dispatchDetail } from '@/api/dispatch'
import Cron from '@/components/Cron.vue'
const dispatchF = reactive(new dispatchClass())
const requestTypeF = reactive(new requestTypeClass())
const sysTypeF = reactive(new sysTypeClass())
const dispatchRef = ref()
const editRef = ref()
const search = () => {
  dispatchPage(dispatchF.dispatchS).then(res => {
    dispatchF.tableData = res.data.records as dispatchInfo[]
    dispatchF.dispatchS.page = res.data.current
    dispatchF.dispatchS.size = res.data.size
    dispatchF.dispatchS.total = res.data.total
  })
}
const resetS = (form: FormInstance | undefined) => {
  if (!form) return
  form.resetFields()
}
const opendialog = () => {
  dispatchF.dialogFormVisible = true
}
const resetE = (form: FormInstance | undefined) => {
  if (!form) return
  form.resetFields()
  dispatchF.dispatchEdit = {} as dispatchEditInfo
}
const save = () => {
  if (dispatchF.dispatchEdit.id) {
    dispatchUpdate(dispatchF.dispatchEdit).then(res => {
      dispatchF.dialogFormVisible = false
      search()
      ElMessage({
        message: '更新调度信息成功',
        type: 'success'
      })
    })
  } else {
    dispatchSave(dispatchF.dispatchEdit).then(res => {
      dispatchF.dialogFormVisible = false
      search()
      ElMessage({
        message: '添加调度信息成功',
        type: 'success'
      })
    })
  }
}
const deletes = (id: any) => {
  deleteDispatch(id).then(res => {
    search()
    ElMessage({
      message: '删除调度任务成功',
      type: 'success'
    })
  })
}
const chengStatus = (data: dispatchInfo) => {
  let msg = '关闭任务成功'
  if (data.open) {
    msg = '开启任务成功'
  }
  dispatchPauseOrRes(data.id, !data.open).then(res => {
    search()
    ElMessage({
      message: msg,
      type: 'success'
    })
  })
}
const edit = (id: any) => {
  dispatchDetail(id).then(res => {
    dispatchF.dialogFormVisible = true
    dispatchF.dispatchEdit = res.data as dispatchEditInfo
    dispatchF.dispatchEdit.requestType = res.data.requestType.name
    dispatchF.dispatchEdit.sysName = res.data.sysName.name
    dispatchF.dispatchEdit.type = res.data.type.name
  })
}
onMounted(() => {
  search()
})
</script>
<style lang="scss" scoped>
.el-select {
  width: 200px;
}
</style>
